// import {
// 	getOAuthUrl,
// 	wechat
// } from '@/services/user';
import { getOAuthUrl, wechat } from "@/services/login";

import { share } from "@/utils/share.js";

export default async function loginto(e) {
  // #ifdef H5
  var ua = navigator.userAgent.toLowerCase();
  var isWXWork = ua.match(/wxwork/i) == "wxwork";
  var isWeixin = !isWXWork && ua.match(/micromessenger/i) == "micromessenger";
  var isMobile = false;
  var isDesktop = false;
  isMobile = true;
  if (
    navigator.userAgent.match(
      /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|IEMobile)/i
    )
  ) {
    isMobile = true;
  } else {
    isDesktop = true;
  }
  var query = window && window.location.href.split("?")[1];
  var params = {};
  if (query) {
    var pairs = query.split("&");
    pairs.forEach(function (pair) {
      var parts = pair.split("=");
      params[decodeURIComponent(parts[0])] = decodeURIComponent(parts[1]);
    });
  }

  if (params.send_id) {
    uni.removeStorageSync("Authorization");
  }

  if (isWeixin) {
    uni.setStorageSync("loginType", 1);
    if (params.code) {
      // return
      if (uni.getStorageSync("Authorization")) {
        return;
      }
      if (params.referrer_id) {
        uni.setStorageSync(
          "referrer_id",
          params.referrer_id ? params.referrer_id : 0
        );
      }
      let res = await wechat({
        code: params.code,
        referrer_id: uni.getStorageSync("referrer_id")
          ? uni.getStorageSync("referrer_id")
          : 0,
      });
      if (res.code == 200) {
        if (res.data.token && res.data.token.token && res.data.ssid) {
          uni.setStorageSync("ssid", res.data.ssid);
          uni.setStorageSync("Authorization", res.data.token.token);
          return;
        }
        if (res.data.ssid) {
          // uni.getStorageSync("ssid");

          uni.setStorageSync("ssid", res.data.ssid);
          if (params.card_id) {
            return;
          }
          // uni.redirectTo({
          //   url:
          //     "/pages/login/login?referrer_id=" +
          //     (uni.getStorageSync("referrer_id")
          //       ? uni.getStorageSync("referrer_id")
          //       : 0),
          // });
        } else if (res.data.token.token) {
          uni.setStorageSync("Authorization", res.data.token.token);
        }
      }
      uni.setStorageSync("userInfo", res.data);
      var userid = uni.getStorageSync("userInfo")
        ? uni.getStorageSync("userInfo").id
        : 0;

      share(
        "明松旺",
        window.location.origin + "/pages/health/health?referrer_id=" + userid,
        "https://lmt-app-cloud.yibaitao.com/20240420/a80079ef-050f-2f13-b6a3-82d0fa552d29.png"
      );

      return;
    } else {
      if (params.code) {
        return;
      }
      if (uni.getStorageSync("Authorization")) {
        return;
      }

      let redirectUrl = window.location.href;

      let res = await getOAuthUrl({
        redirectUrl: redirectUrl,
      });
      window.location.href = res.data.redirectUrl;

      // window.location.replace(res.result.redirectUrl)
    }
  } else {
    var userid = uni.getStorageSync("userInfo")
      ? uni.getStorageSync("userInfo").id
      : 0;

    share(
      "明松旺",
      window.location.origin + "/pages/health/health?referrer_id=" + userid,
      "https://m-cloud.yibaitao.com/60/2aac23d4403464e08cbb4bde11881c.jpg"
    );
  }
  // #endif
}
